import { v3, Vec3 } from "cc";
import { Ab_DataTransfer_bA } from "../../A-FRAME/A-LIB/lib.b.data";
import Ab_GlobalDC_bA from "../../A-FRAME/common/data.global.center";
import { Ab_APP_NAME_bA } from "../../A-FRAME/frame.config";
import { Ab_NCAPIURI_bA, Ab_NCRequestParam_bA } from "../api/api.config.client";
import Ab_get_proxy_status_bA from "../data/data.status";
import Ab_get_proxy_trigger_bA from "../data/data.trigger";
import Ab_get_proxy_user_bA from "../data/data.user";
import { Ab_FORCE_POP_bA } from "../game.config";
import { Ab_AudioManager_bA } from "./audio-manager";
import Ab_Config_bA from "./config.manager";
import Ab_GameAssets_bA from "./game.assets";

export default class Ab_GameDC_bA {
	private static Ab__user__bA: ReturnType<typeof Ab_get_proxy_user_bA>;
	private static Ab__trigger__bA: ReturnType<typeof Ab_get_proxy_trigger_bA>;
	private static Ab__status__bA: ReturnType<typeof Ab_get_proxy_status_bA>;

	public static get Ab_User_bA(){
		return Ab_GameDC_bA.Ab__user__bA;
	}
	public static get Ab_Trigger_bA(){
		return Ab_GameDC_bA.Ab__trigger__bA;
	}
	public static get Ab_Status_bA(){
		return Ab_GameDC_bA.Ab__status__bA;
	}

	private static __fruit__ = 0;
	public static get Ab_fruit_bA(){
		return Ab_GameDC_bA.__fruit__;
	}

	public static Ab_init_bA(){
		Ab_GameDC_bA.Ab__user__bA = Ab_get_proxy_user_bA('game_user', Ab_APP_NAME_bA);
		Ab_GameDC_bA.Ab__trigger__bA = Ab_get_proxy_trigger_bA('game_trigger');
		Ab_GameDC_bA.Ab__status__bA = Ab_get_proxy_status_bA('game_status');

		Ab_GameDC_bA.Ab_rank_list_bA = abi.Ab_local_bA.Ab_read_bA(Ab_APP_NAME_bA + 'rank_list');

		Ab_GameDC_bA.Ab_listening_bA();
	}

	public static Ab_listening_bA(){
		Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_round_bA', (_, round)=>{
			Ab_GameDC_bA.__fruit__ = Reflect.get(Ab_Config_bA.Ab_officialConf_bA, 'fusion'+round);
			Ab_GameDC_bA.Ab_refreshStatistics_bA(Date.now());
		});

		Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_bgm_bA', (_, pick)=>{
			Ab_AudioManager_bA.Ab_mute_bA(!pick);
		}, false);

		Ab_GlobalDC_bA.Ab_interval_bA(60 * 1000, (td, now)=>Ab_GameDC_bA.Ab_refreshStatistics_bA(now));
	}

	public static Ab_checkTask_bA(type: 'remove'|'round', id: number){
		return new RegExp('(?<!\\d)'+id+'(?!\\d)', 'gm').test(type=='round' ? Ab_GameDC_bA.Ab_User_bA.Ab_task_round_bA : Ab_GameDC_bA.Ab_User_bA.Ab_task_remove_bA);
	}

	public static Ab_recordTask_bA(type: 'remove'|'round', id: number|string){
		let str = type=='round' ? Ab_GameDC_bA.Ab_User_bA.Ab_task_round_bA : Ab_GameDC_bA.Ab_User_bA.Ab_task_remove_bA;
		if(str!='') str += ';';
		if(type=='round') Ab_GameDC_bA.Ab_User_bA.Ab_task_round_bA = str+id;
		else Ab_GameDC_bA.Ab_User_bA.Ab_task_remove_bA = str+id;
	}

	private static Ab_rank_list_bA: Array<{nickname: string, round: number, symbol: number, place: number}>;
	public static get Ab_RankList_bA(){
		return Ab_GameDC_bA.Ab_rank_list_bA;
	}
	/** 初始化排行榜数据 */
	public static Ab_initRankData_bA(name_list: string[], rank_a: number, rank_b: number){
		let list: Array<{nickname: string, round: number, symbol: number, place: number}> = [];

		list.push({
			nickname: abi.Ab_array_bA.Ab_random_bA(name_list),
			round: abi.Ab_number_bA.Ab_random_bA(50, 80, 0),
			symbol: 0,
			place: 0
		});

		list[0].symbol = list[0].round * abi.Ab_number_bA.Ab_random_bA(rank_a, rank_b, 0);

		while(list.length<100){
			list.unshift({nickname: abi.Ab_array_bA.Ab_random_bA(name_list), round: 0, symbol: 0, place: 0});
			list[0].round = list[1].round + abi.Ab_number_bA.Ab_random_bA(0, 5, 0);
			list[0].symbol = list[0].round * abi.Ab_number_bA.Ab_random_bA(rank_a, rank_b, 0);
		}

		//Ab_GameDC_bA._rank_list_ = list.sort((a, b)=>b.round - a.round);
		Ab_GameDC_bA.Ab_rank_list_bA = list.sort((a, b)=>b.round - a.round);

		list.forEach((el, i)=>el.place = i+1);

		abi.Ab_local_bA.Ab_save_bA(Ab_APP_NAME_bA + 'rank_list', list);

		return list;
	}

	private static __reap_symbol_vec__ = v3();
	private static __reap_symbol_vec_ref__: Vec3;
	public static get Ab_reapSymbolVec_bA(){
		return this.__reap_symbol_vec_ref__;
	}
	public static Ab_reapSymbol_bA(num: number, world_position: Vec3){
		if(world_position){
			Ab_GameDC_bA.__reap_symbol_vec__.set(world_position);
			Ab_GameDC_bA.__reap_symbol_vec_ref__ = Ab_GameDC_bA.__reap_symbol_vec__;
		}else Ab_GameDC_bA.__reap_symbol_vec_ref__ = null;
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_CHANGE_SYMBOL_bA = num;
	}

	private static __reap_diamond_vec__ = v3();
	private static __reap_diamond_vec_ref__: Vec3;
	public static get Ab_reapDiamondVec_bA(){
		return this.__reap_diamond_vec_ref__;
	}
	public static Ab_reapDiamond_bA(num: number, world_position: Vec3){
		if(world_position){
			Ab_GameDC_bA.__reap_diamond_vec__.set(world_position);
			Ab_GameDC_bA.__reap_diamond_vec_ref__ = Ab_GameDC_bA.__reap_diamond_vec__;
		}else Ab_GameDC_bA.__reap_diamond_vec_ref__ = null;
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_CHANGE_DIAMOND_bA = num;
	}

	/** 检测并记录强弹 */
	public static Ab_checkForcePop_bA(): Ab_NCRequestParam_bA<Ab_NCAPIURI_bA.Ab_AD_bA, 'Ab_type_bA'>{
		let count = Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA;

		if(!(count>0) || count%3!=0) return void 0;

		let rp_count = Math.floor(count / 3);
		let conf = Ab_FORCE_POP_bA.find(el=>rp_count.Ab_inRange_bA(el.range[0], el.range[1]));

		let force_rp = (abi.Ab_local_bA.Ab_read_bA<number>(Ab_APP_NAME_bA+'force-rp') ?? 0) + 1;
		let force_idx = (abi.Ab_local_bA.Ab_read_bA<number>(Ab_APP_NAME_bA+'force-idx') ?? 0);


		if(conf.range[0] == rp_count) force_idx = 0;

		let force = force_rp >= conf.interval[force_idx];

		let type: Ab_NCRequestParam_bA<Ab_NCAPIURI_bA.Ab_AD_bA, 'Ab_type_bA'>;
		if(force){
			force_idx = (force_idx + 1) % conf.interval.length;
			force_rp = 0;

			let force_count = (abi.Ab_local_bA.Ab_read_bA<number>(Ab_APP_NAME_bA+'force-count') ?? 0) + 1;
			if(force_count <= 5){
				type = 'full-screen';
				abi.Ab_local_bA.Ab_save_bA(Ab_APP_NAME_bA+'force-count', force_count);
			}else type = 'incentive';
		}

		abi.Ab_local_bA.Ab_save_bA(Ab_APP_NAME_bA+'force-rp', force_rp);
		abi.Ab_local_bA.Ab_save_bA(Ab_APP_NAME_bA+'force-idx', force_idx);

		return type;
	}

	/**
	 * 刷新统计信息
	 * @param now
	 */
	private static Ab_refreshStatistics_bA(now: number){
		let pass_time = now - new Date(now).setHours(0, 0, 0, 0);
		let round = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA;

		Ab_GameDC_bA.Ab_Status_bA.Ab_statistics_count_bA = Math.round(round<3 ? 20 : 23/** 每分钟的人数增长（与关卡相关的配置） */ * pass_time / 1000 / 60);
		Ab_GameDC_bA.Ab_Status_bA.Ab_statistics_round_bA = round>=4 ? abi.Ab_number_bA.Ab_random_bA(20, 21, 2) : 1;
		Ab_GameDC_bA.Ab_Status_bA.Ab_statistics_free_bA = round<4 ? Reflect.get(Ab_Config_bA.Ab_officialConf_bA, 'statistics'+round) * Ab_GameDC_bA.Ab_Status_bA.Ab_statistics_round_bA : abi.Ab_number_bA.Ab_random_bA(Ab_Config_bA.Ab_officialConf_bA.statistics4_a, Ab_Config_bA.Ab_officialConf_bA.statistics4_b, 2);
	}

	private static Ab_live_comments_list_bA: Array<{nickname: string, count: number, symbol: number, channel: string}> = [];
	/**
	 * 获取指定数量的弹幕
	 * @param count
	 */
	public static Ab_getLiveComments_bA(count?: number){
		count = count ?? Ab_GameDC_bA.Ab_live_comments_list_bA.length;
		while(Ab_GameDC_bA.Ab_live_comments_list_bA.length < count) Ab_GameDC_bA.Ab_live_comments_list_bA.push(Ab_GameDC_bA.Ab_createLiveComments_bA(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA));
		return Ab_GameDC_bA.Ab_live_comments_list_bA.slice(0, count);
	}
	/**
	 * 根据指定的关卡，创建一条弹幕文本
	 * @param round
	 */
	private static Ab_createLiveComments_bA(round: number){
		let nickname = Ab_GameAssets_bA.Ab_getNickname_bA();
		let count = round>=4 ? abi.Ab_number_bA.Ab_random_bA(15, 30, 0) : 1;
		let symbol = 0;
		if(round>=4){
			let coe = abi.Ab_number_bA.Ab_random_bA(Ab_Config_bA.Ab_officialConf_bA.live4_a, Ab_Config_bA.Ab_officialConf_bA.live4_b, 0);
			symbol = count * coe;
		}else symbol = Reflect.get(Ab_Config_bA.Ab_officialConf_bA, 'live'+round);

		//hnc.log.tag('创建弹幕: green;' + [
		//	['关卡', round+1],
		//	['挑战次数', count],
		//	['金额', Ab_GameDC_bA.symbolString(symbol)],
		//	['昵称', nickname],
		//].map(([a, b])=>a+':tan;'+b+':azure').join(';'));
		return {nickname, count, symbol, channel: abi.Ab_array_bA.Ab_random_bA(Ab_Config_bA.Ab_countryConf_bA.channel)};
	}

	/**
	 * 从顶部丢弃指定数量的弹幕数据
	 * @param count
	 */
	public static Ab_throwAwayLiveComments_bA(count?: number){

		let new_list = abi.Ab_array_bA.Ab_create_bA(Ab_GameDC_bA.Ab_live_comments_list_bA.splice(0, count??1).length, ()=>Ab_GameDC_bA.Ab_createLiveComments_bA(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA));

		Ab_GameDC_bA.Ab_live_comments_list_bA.push(...new_list);
	}

	public static Ab_checkGuideID_bA(id: number){
		let reg = new RegExp('(?<!\\d)'+id+'(?!\\d)', 'gm');
		return reg.test(Ab_GameDC_bA.Ab_User_bA.Ab_guide_record_bA);
	}

	public static Ab_DoneGuidID_bA(id: number){
		if(Ab_GameDC_bA.Ab_checkGuideID_bA(id)) return void 0;
		if(Ab_GameDC_bA.Ab_User_bA.Ab_guide_record_bA=='') Ab_GameDC_bA.Ab_User_bA.Ab_guide_record_bA += id;
		else Ab_GameDC_bA.Ab_User_bA.Ab_guide_record_bA += ':'+id;
	}

	public static Ab_CleanGuideID_bA(...list: number[]){
		if(list.length>0){
			let guid_done = Ab_GameDC_bA.Ab_User_bA.Ab_guide_record_bA;
			list.forEach(id=>{
				let reg = new RegExp('(?<!\\d)'+id+'(?!\\d)', 'gm');
				guid_done = guid_done.replace(reg, '');
			});
			Ab_GameDC_bA.Ab_User_bA.Ab_guide_record_bA = guid_done.replaceAll(/(?<!\d):/gm, '').replaceAll(/:(?!\d)/gm, '');
		}
	}
}